<?php

use think\facade\Db;

/**
 * 重写Url生成
 * @param string $url 路由地址
 * @param array $vars 变量
 * @param bool|string $suffix 生成的URL后缀
 * @param bool|string $domain 域名
 */
function url(string $url = '', array $vars = [], $suffix = true, $domain = false): think\route\Url
{
    $rule = explode('/', $url);
    if (count($rule) > 1) {
        return think\facade\Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
    } else {
        $url = env('APP_MAP.ADMIN_MAP') . DIRECTORY_SEPARATOR . request()->controller . DIRECTORY_SEPARATOR . $url;
        return think\facade\Route::buildUrl($url, $vars)->suffix($suffix)->domain($domain);
    }
}

function get_pay_way($value)
{
    return empty($value) ? '未支付' : match ($value) {
        'alipay' => '支付宝',
        'wechat' => '微信支付',
        default => \app\common\model\Assets::getFields(3)[$value]
    };
}



function getBonusTitle($type)
{
    if ($type == 1) {
        return [1 => '销售提成', 2 => '市场补贴', 3 => '管理补贴', 4 => '管理平级', 5 => '代理补贴', 6 => '复消平级', 7 => '代理分红'];
    } else {
        return ['sum_1' => '销售提成', 'sum_2' => '市场补贴', 'sum_3' => '管理补贴', 'sum_4' => '管理平级', 'sum_5' => '代理补贴', 'sum_6' => '复消平级', 'sum_7' => '代理分红'];
    }

}

function getCount($fetchSql)
{
    $fetchSql = "SELECT count(*) as tp_count " . strstr($fetchSql, "FROM");
    return Db::query($fetchSql)[0]['tp_count'];
}

function generate($length, $prefix = '', $type = 1)
{
    if ($type == 1) {
        $code = rand(100000, 999999);
    } else {
        $chars = array('a', 'b', '1', 'd', 'e', '2', 'g', 'h',
            'i', '3', 'k', 'l', '4', 'n', 'o', '5', 'q', 'r', 's',
            't', 'u', '6', 'w', 'x', '7', 'z', 'c', '8', 'j', 'm',
            'p', 'v', 'y', 'f', '9');
        $keys = array_rand($chars, $length);
        $code = '';
        for ($i = 0; $i < $length; $i++) {
            $code .= $chars[$keys[$i]];
        }
    }
    if ($prefix) $code = $prefix . $code;
    $is = \app\common\model\User::where('code', $code)->count();
    return $is ? generate(6, $prefix, $type) : $code;
}

function generateRandom($length)
{
    $chars = array('a', 'b', '1', 'd', 'e', '2', 'g', 'h',
        'i', '3', 'k', 'l', '4', 'n', 'o', '5', 'q', 'r', 's',
        't', 'u', '6', 'w', 'x', '7', 'z', 'c', '8', 'j', 'm',
        'p', 'v', 'y', 'f', '9');
    $keys = array_rand($chars, $length);
    $code = '';
    for ($i = 0; $i < $length; $i++) {
        $code .= $chars[$keys[$i]];
    }

    return $code;

}


//格式化富文本内容
function handleRichText($content)
{
    $url = request()->scheme() . '://' . $_SERVER['SERVER_NAME'] . '/uploads/';
    $content = htmlspecialchars_decode(str_replace('/uploads/', $url, $content));
    return str_replace('\\', '', $content);
}

function handleImg($src)
{
    if (empty($src)) {
        return '';
    }
    if (is_array($src)) {
        foreach ($src as &$v) {
            $v = request()->scheme() . '://' . $_SERVER['SERVER_NAME'] . $v;
        }
        return $src;
    } else {
        return request()->scheme() . '://' . $_SERVER['SERVER_NAME'] . $src;
    }

}

function filter_Emoji($str)
{
    $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str);

    return $str;
}

function queryIp($ip)
{

    $url = "http://whois.pconline.com.cn/jsFunction.jsp?ip=" . $ip;
    $ch = curl_init();
    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    $info = iconv('GB2312', 'UTF-8', $output); //因为是js调用 所以接收到的信息为字符串，注意编码格式

    return get_between($info, "('", "',");
}

function get_between($input, $start, $end)
{
    return substr($input, strlen($start) + strpos($input, $start), (strlen($input) - strpos($input, $end)) * (-1));
}